<div class="layui-form layui-row" lay-filter="form_message">
  <script type="text/html" template lay-done="layui.data.done(d.params)">
    <div class="layui-card-body layui-row layui-col-space10">

      <div class="layui-form-item">
        <label class="layui-form-label">站内信标题</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" lay-verify="required" name="title"
                 value="{{d.params.title || ''}}">
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">发送名单</label>
        <div class="layui-input-block" style="position:relative;">
          <button type="button" class="layui-btn" id="receiverUpload">
            <i class="layui-icon">&#xe67c;</i>上传名单
          </button>
          <div class="layui-inline layui-word-aux" style="padding-top: 25px">
            <a href="/assets/excel/站内信发送名单模板.xls" class="layui-font-blue" style="text-decoration:underline;">下载模版</a>
            <a class="layui-font-green layui-hide" id="uploadTips">上传成功!</a>
          </div>
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label">发布时间</label>
        <div class="layui-input-inline">
          <input type="text" id="publish_time" class="layui-input" name="publish_time" placeholder="定时发布时间"
                 autocomplete="off" value="{{d.params.publish_time || ''}}">
        </div>
        <div class="layui-inline">
          <div class="layui-form-mid layui-word-aux">
            不填则提交后马上发布
          </div>
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label form-required">消息模板</label>
        <div class="layui-word-aux layui-input-block layui-font-red">
          注: 消息模板支持设置最多三个预留关键字, {keyword1}, {keyword2}和{keyword3}, 通过在用户名单中设置对应的内容，即可实现每个用户发送不同的定制化内容
        </div>
      </div>
      <div class="layui-form-item">
        <label class="layui-form-label"></label>
        <div class="layui-input-inline" style="width: 600px">
          <div id="content">
            {{d.params.content || ''}}
          </div>
        </div>
      </div>

      <div class="layui-card-body layui-row layui-col-space10">
        <div class="layui-col-md6">
          <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-inline">
              <a class="layui-btn" lay-submit lay-filter="submit_message">保存</a>
            </div>
          </div>
        </div>
      </div>
    </div>
  </script>
</div>

<script>
  layui.data.done = function () {

    layui.use(['laydate', 'wangEditor'], function () {
      let $ = layui.$
        , admin = layui.admin
        , laydate = layui.laydate
        , upload = layui.upload
        , editor = layui.wangEditor
        , form = layui.form
        , table = layui.table

      //日历初始化
      laydate.render({
        elem: '#publish_time'
        , type: 'datetime'
      });

      let upload_sn = '';
      let has_keyword1 = false;
      let has_keyword2 = false;
      let has_keyword3 = false;
      let uploadInst = upload.render({
        elem: '#receiverUpload' //绑定元素
        , url: 'agent-admin/upload_receivers' //上传接口
        , exts: 'xls|xlsx|XLS|XLSX'
        , method: 'POST'
        , headers: {
          Authorization: admin.getAccessToken('Bearer')
        }
        , done: function (res) {
          if (res.code !== 0) {
            layer.msg(res.msg);
            return false;
          }
          upload_sn = res.data.upload_sn;
          has_keyword1 = res.data.has_keyword1
          has_keyword2 = res.data.has_keyword2
          has_keyword3 = res.data.has_keyword3
          $('#uploadTips').removeClass('layui-hide');
        }
      });

      //编辑器
      editor.render({
        elem: "#content"
        , height: 300
        , name: 'content'
        , url: 'editor/images'
      });

      //提交表单
      form.on('submit(submit_message)', function (d) {
        let field = d.field;
        if (upload_sn === '') {
          layer.msg('请上传发送名单')
          return
        }
        if (has_keyword1 && !field.content.includes('{keyword1}')) {
          layer.msg('发送名单中上传了keyword1,但消息内容中不包含{keyword1}')
          return
        }
        if (has_keyword2 && !field.content.includes('{keyword2}')) {
          layer.msg('发送名单中上传了keyword2,但消息内容中不包含{keyword2}')
          return
        }
        if (has_keyword3 && !field.content.includes('{keyword3}')) {
          layer.msg('发送名单中上传了keyword3,但消息内容中不包含{keyword3}')
          return
        }
        field.upload_sn = upload_sn
        admin.post('agent-admin/message_templates', field, function (res) {
          layer.closeAll();
          table.reload('table_message');
        }, {hasLoading: true})
      })

    })
  }
</script>
